<?php

namespace backend\services\recharge;

use backend\extensions\ApiBaseService;
use backend\models\recharge\RechargeOrder;
use backend\models\user\User;
// use api\services\payment\wechat\WechatService;

/**
 * Desc 充值订单服务类
 * @author HUI
 */
class RechargeOrderService extends ApiBaseService
{

    /**
     * 列表
     * @param array $params 参数 
     * **/
    public static function getList($params)
    {
        //检索条件
        $condition = ['AND'];
        if ($params['name']) {
            $condition[] = ['like', 'name', $params['name']];
        }
        if ($params['state']) {
            $condition[] = ['=', 'state', $params['state']];
        }
        if ($params['phone']) {
            $condition[] = ['=', 'user_id', User::findOne(['phone'=>$params['phone']])['id']];
        }
        $list = RechargeOrder::getDataAll($condition, $params['page'], $params['page_size']);
        foreach ($list['list'] as &$value) {
            $user = User::findOne(['id' => $value['user_id']]);
            $value['phone'] = $user['phone'];
            $value['state_name'] = RechargeOrder::$state[$value['state']];
        }
        return $list;
    }

    /**
     * 新增
     * @param array $params 参数 
     * **/
    public static function create($params)
    {
        if (RechargeOrder::create($params) == false) {
            return self::jsonError('保存失败');
        }
        return self::jsonSuccess();
    }

    /**
     * 新增
     * @param array $params 参数 
     * **/
    public static function update($params)
    {
        $condition = ['id' => $params['id']];
        unset($params['id']);
        if (RechargeOrder::updateData($params, $condition) === false) {
            return self::jsonError('更新失败');
        }
        return self::jsonSuccess();
    }

    /**
     * 删除
     * @param array $params 参数 
     * **/
    public static function delete($params)
    {
        $condition = ['id' => $params['id']];
        if (RechargeOrder::del($condition) == false) {
            return self::jsonError('删除失败');
        }
        return self::jsonSuccess();
    }

    /**
     * 详情
     * @param array $params 参数 
     * **/
    public static function detail()
    {
        $condition = [];
        $data = RechargeOrder::getDataOne($condition, '', []);
        return self::jsonSuccess($data);
    }
    
    /**
     * 退款操作
     * @param array $params 参数 
     * **/
   public static function review($params) {
        try {
            $order = RechargeOrder::findOne(['order_sn' => $params['order_sn']]);
            if (empty($order)) {
                throw new \Exception('订单信息异常');
            }
            if ($order['state'] != 4) {
                throw new \Exception('该订单状态无法操作');
            }
            //拒绝
            $order->state = 6;
            if($params['type'] == 1){
                // $result = WechatService::refund($order);
                // if ($result['code'] != 200) {
                //     throw new \Exception($result['message']);
                // }
                $order->refund_time = date('Y-m-d H:i:s');
                $order->state = 5;
            }
            if(empty($order->save())){
                throw new \Exception('退款异常');
            }
            return self::jsonSuccess();
        } catch (\Exception $exc) {
            return self::jsonError($exc->getMessage());
        }
    }
}
